using System;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Reflection.Emit;
using System.Resources;

namespace ns20
{
	internal static class Class164
	{
		private static readonly object object_0 = new object();

		private static readonly Class166 class166_0 = new Class166();

		private static ModuleBuilder moduleBuilder_0;

		private static ModuleBuilder ModuleBuilder_0
		{
			get
			{
				smethod_0();
				return moduleBuilder_0;
			}
		}

		private static void smethod_0()
		{
			if (!(moduleBuilder_0 == null))
			{
				return;
			}
			lock (object_0)
			{
				if (moduleBuilder_0 == null)
				{
					AssemblyName assemblyName = new AssemblyName("Newtonsoft.Json.Dynamic");
					assemblyName.KeyPair = new StrongNameKeyPair(smethod_1());
					AssemblyBuilder assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run);
					moduleBuilder_0 = assemblyBuilder.DefineDynamicModule("Newtonsoft.Json.DynamicModule", emitSymbolInfo: false);
				}
			}
		}

		private static byte[] smethod_1()
		{
			using Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("Newtonsoft.Json.Dynamic.snk");
			if (stream == null)
			{
				throw new MissingManifestResourceException("Should have Newtonsoft.Json.Dynamic.snk as an embedded resource.");
			}
			int num = (int)stream.Length;
			byte[] array = new byte[num];
			stream.Read(array, 0, num);
			return array;
		}

		public static Type smethod_2(Type type_0, Type type_1)
		{
			Type type = class166_0.method_0(type_0, type_1);
			if (type == null)
			{
				lock (object_0)
				{
					type = class166_0.method_0(type_0, type_1);
					if (!(type == null))
					{
						return type;
					}
					type = smethod_4(type_0, type_1);
					class166_0.method_1(type_0, type_1, type);
					return type;
				}
			}
			return type;
		}

		public static object smethod_3(object object_1)
		{
			Class163 @class = object_1 as Class163;
			if (@class == null)
			{
				throw new ArgumentException("Object is not a wrapper.", "wrapper");
			}
			return @class.object_0;
		}

		private static Type smethod_4(Type type_0, Type type_1)
		{
			TypeBuilder typeBuilder = ModuleBuilder_0.DefineType("{0}_{1}_Wrapper".smethod_1(CultureInfo.InvariantCulture, type_0.Name, type_1.Name), TypeAttributes.Sealed, typeof(Class163), new Type[1] { type_0 });
			Class165 @class = new Class165(type_1, typeBuilder);
			foreach (MethodInfo item in type_0.smethod_18())
			{
				@class.method_0(item);
			}
			return typeBuilder.CreateType();
		}

		public static T smethod_5<T>(object object_1) where T : class
		{
			Type type = smethod_2(typeof(T), object_1.GetType());
			Class163 @class = (Class163)Activator.CreateInstance(type);
			@class.object_0 = object_1;
			return @class as T;
		}
	}
}
